package com.car.advertising.activity.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.car.advertising.CarApplication;
import com.car.advertising.activity.CashAccountActivity;
import com.car.advertising.activity.IncomeDetailsActivity;
import com.car.advertising.activity.PersonalInformationActivity;
import com.car.advertising.activity.TaskRecordActivity;
import com.car.advertising.activity.WithdrawalsActivity;
import com.car.advertising.activity.adapter.MePageAdapter;
import com.car.advertising.db.UserInfoBiz;
import com.car.advertising.db.UserInfoEntiy;
import com.car.advertising.entity.MeEntity;
import com.car.advertising.entity.bean.AmountBean;
import com.support.widget.JumpManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.fragment.BaseRecyclerviewFreshFragment;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MePageFragment extends BaseRecyclerviewFreshFragment implements MePageAdapter.MeClickListener {

    private List<MeEntity> meList = new ArrayList<>();
    private MePageAdapter mePageAdapter;
    private UserInfoEntiy userInfoEntiy;

    @Override
    protected void init(Bundle savedInstanceState) {
        setPullDownEnabled(false);// 禁掉下拉加载
        setPullUpEnabled(false);// 禁掉上拉加载

        userInfoEntiy = UserInfoBiz.getInstance(getThis()).loadData();
        meList = getMeList();

        mePageAdapter = new MePageAdapter(getThis(), meList, this);
        initRecyclerView(mePageAdapter);
        mePageAdapter.setOnItemClickListener(meViewItemClick);
    }

    private List<MeEntity> getMeList() {
        ArrayList<MeEntity> meEntity = new ArrayList<>();
        meEntity.add(new MeEntity(0, getAmountBean(), null, false, null));
        meEntity.add(new MeEntity(1, null, "任务记录", false, null));
        meEntity.add(new MeEntity(2, null, "收益明细", false, null));
        meEntity.add(new MeEntity(3, null, "提现账号", false, null));
        meEntity.add(new MeEntity(4, null, "优惠券", true, "5张优惠券"));
        meEntity.add(new MeEntity(5, null, "活动中心", true, null));
        return meEntity;
    }

    private AmountBean getAmountBean() {
        AmountBean amountBean = new AmountBean();
        if (userInfoEntiy != null) {
            amountBean.setUserName(userInfoEntiy.getUserName());
        } else {
            amountBean.setUserName("接米");
        }

        amountBean.setTotalAmount("421087");
        amountBean.setLastTimeAmount("8495281");
        amountBean.setCumulativeAmount("172946");
        return amountBean;
    }

    @Override
    public void onMeHeadCLick(char type) {
        switch (type) {
            case 'i'://
                if (!TextUtils.isEmpty(CarApplication.token)) {
                    JumpManager.gotoActivity(getContext(), PersonalInformationActivity.class, false);
                } else {
                    ToastUtil.show(getContext(), "请先登录后再尝试", Toast.LENGTH_SHORT);
                }
                break;
            case 't'://
                ToastUtil.show(getContext(), "总资产", Toast.LENGTH_SHORT);
                break;
            case 'l'://
                ToastUtil.show(getContext(), "上次收益", Toast.LENGTH_SHORT);
                break;
            case 'c'://
                ToastUtil.show(getContext(), "累计收益", Toast.LENGTH_SHORT);
                break;
            case 'w'://
                JumpManager.gotoActivity(getContext(), WithdrawalsActivity.class, false);
                break;
            default:
                break;
        }
    }

    private OnRecyclerViewItemClickListener meViewItemClick = new OnRecyclerViewItemClickListener() {

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            switch (position) {
                case 0:
                    break;

                case 1:// 任务记录
                    JumpManager.gotoActivity(getContext(), TaskRecordActivity.class, false);
                    break;

                case 2:// 收益明细  IncomeDetailsActivity
                    JumpManager.gotoActivity(getContext(), IncomeDetailsActivity.class, false);
                    break;

                case 3:// 支付宝
                    JumpManager.gotoActivity(getContext(), CashAccountActivity.class, false);
                    break;

                case 4:// 优惠券
                    ToastUtil.show(getContext(), "优惠券", Toast.LENGTH_SHORT);
                    break;

                case 5:// 活动中心
                    ToastUtil.show(getContext(), "活动中心", Toast.LENGTH_SHORT);
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onNoDoubleClick(View v) {

    }
}
